package cn.icatw.leetcode.editor.cn;
//给你一个二叉树的根节点 root ，按 任意顺序 ，返回所有从根节点到叶子节点的路径。
//
// 叶子节点 是指没有子节点的节点。
//
// 示例 1：
//
//
//输入：root = [1,2,3,null,5]
//输出：["1->2->5","1->3"]
//
//
// 示例 2：
//
//
//输入：root = [1]
//输出：["1"]
//
//
//
//
// 提示：
//
//
// 树中节点的数目在范围 [1, 100] 内
// -100 <= Node.val <= 100
//
//
// Related Topics 树 深度优先搜索 字符串 回溯 二叉树 👍 1151 👎 0


import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

//Java：二叉树的所有路径
public class T257_BinaryTreePaths {
    public static void main(String[] args) {
        Solution solution = new T257_BinaryTreePaths().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> paths = new ArrayList<>();
            if (root != null) {
                //1.递归法 构造路径
                //constructPaths(root, "", paths);
                //    2.迭代法
                Stack<Object> stack = new Stack<>();
                stack.push(root);
                stack.push(root.val + "");
                while (!stack.isEmpty()) {
                    String path = (String) stack.pop();
                    TreeNode node = (TreeNode) stack.pop();
                    //叶子结点
                    if (node.left == null && node.right == null) {
                        paths.add(path);
                    }
                    if (node.right != null) {
                        stack.push(node.right);
                        stack.push(path + "->" + node.right.val);
                    }
                    if (node.left != null) {
                        stack.push(node.left);
                        stack.push(path + "->" + node.left.val);
                    }
                }
            }
            return paths;
        }

        /**
         * 递归法 构造路径
         *
         * @param root  根
         * @param path  路径
         * @param paths 路径
         */
       /* private void constructPaths(TreeNode root, String path, List<String> paths) {
            //    前序遍历
            if (root != null) {
                //    加入path
                path += Integer.toString(root.val);
                //    判断是否是叶子节点
                if (root.left == null && root.right == null) {
                    paths.add(path);
                } else {
                    path += "->";
                    constructPaths(root.left, path, paths);
                    constructPaths(root.right, path, paths);
                }
            }
        }*/
    }

    //leetcode submit region end(Prohibit modification and deletion)
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
